#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "pthread.h"

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "AWLocalViewport.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWBrain.h"
#include "AWTracerBrain.h"
#include "AWThreadedBrain.h"
#include "AWNOOSBrain.h"

#include "loader.h"

AW_NOOSBrain::AW_NOOSBrain(Ontology *O,FTKBase *Memory,FTKBase *CB,bool pause) : AW_ThreadedBrain(O,Memory,pause) 
{
	class_name->set("AW_NOOSBrain");

	m_casebase=CB;

	m_ontology->new_sort("learn-message","any",1,"case","any");
	m_ontology->new_sort("forget-message","any",1,"case","any");
	m_ontology->new_sort("request-prediction-message","any",2,"problem","any",
															  "strategy","symbol");
	m_ontology->new_sort("request-test-prediction-message","any",2,"problem","any",
																   "strategy","symbol");
	m_ontology->new_sort("request-individual-predict-message","any",1,"problem","any");
	m_ontology->new_sort("request-individual-single-predict-message","any",1,"problem","any");
	m_ontology->new_sort("solution-inform-message","any",1,"solution","any");
	m_ontology->new_sort("solution-test-inform-message","any",1,"solution","any");

	m_ontology->new_sort("problem-prediction","any",5,"problem","any",
													  "solution","any",
													  "support","integer",
													  "justification","any",
													  "agent","symbol");

	m_ontology->new_sort("request-examination-message","any",2,"problem","any",
															   "prediction","problem-prediction");
	m_ontology->new_sort("prediction-examination","any",3,"prediction","problem-prediction",
														  "endorsing-cases","integer",
														  "counter-examples","integer");

	m_ontology->new_sort("learn-message-iba","any",1,"case","any");
	m_ontology->new_sort("request-counterargument","any",3,"jer","problem-prediction",
														   "problem","any",
														   "historic","problem-prediction");
	m_ontology->new_sort("reset-iba","any",0);
	m_ontology->new_sort("reset-test-iba","any",0);

	m_ontology->new_sort("request-iba-collaboration-message","any",1,"problem","any");
	m_ontology->new_sort("request-test-iba-collaboration-message","any",1,"problem","any");
	m_ontology->new_sort("token-iba-message","any",1,"arguments","any");
	m_ontology->new_sort("counterargument-message","any",2,"counterargument","problem-prediction",
														   "agent","symbol");
	m_ontology->new_sort("counterexample-message","any",2,"counterexample","any",
														  "agent","symbol");
	m_ontology->new_sort("iba-prediction-confidence","any",2,"prediction","any",
														     "confidence","float");

	m_ontology->new_sort("request-casebase-size","any",0);

	m_ontology->new_sort("jbartering-cases","any",1,"cases","any");

} /* AW_NOOSBrain::AW_NOOSBrain */ 



AW_NOOSBrain::AW_NOOSBrain(FILE *fp,Ontology *O,FTKBase *Memory,FTKBase *CB,bool pause) : AW_ThreadedBrain(O,Memory,pause) 
{
	class_name->set("AW_NOOSBrain");

	m_casebase=CB;

	m_ontology->new_sort("learn-message","any",1,"case","any");
	m_ontology->new_sort("forget-message","any",1,"case","any");
	m_ontology->new_sort("request-prediction-message","any",2,"problem","any",
															  "strategy","symbol");
	m_ontology->new_sort("request-test-prediction-message","any",2,"problem","any",
																   "strategy","symbol");
	m_ontology->new_sort("request-individual-predict-message","any",1,"problem","any");
	m_ontology->new_sort("request-individual-single-predict-message","any",1,"problem","any");
	m_ontology->new_sort("solution-inform-message","any",1,"solution","any");
	m_ontology->new_sort("solution-test-inform-message","any",1,"solution","any");

	m_ontology->new_sort("problem-prediction","any",4,"problem","any",
													  "solution","any",
													  "support","integer",
													  "justification","any");

	m_ontology->new_sort("request-examination-message","any",2,"problem","any",
															   "prediction","problem-prediction");
	m_ontology->new_sort("prediction-examination","any",3,"prediction","problem-prediction",
														  "endorsing-cases","integer",
														  "counter-examples","integer");

	m_ontology->new_sort("learn-message-iba","any",1,"case","any");
	m_ontology->new_sort("request-counterargument","any",3,"jer","problem-prediction",
														   "problem","any",
														   "historic","problem-prediction");
	m_ontology->new_sort("reset-iba","any",0);
	m_ontology->new_sort("reset-test-iba","any",0);

	m_ontology->new_sort("request-iba-collaboration-message","any",1,"problem","any");
	m_ontology->new_sort("request-test-iba-collaboration-message","any",1,"problem","any");
	m_ontology->new_sort("token-iba-message","any",1,"arguments","any");
	m_ontology->new_sort("counterargument-message","any",2,"counterargument","problem-prediction",
														   "agent","symbol");
	m_ontology->new_sort("counterexample-message","any",2,"counterexample","any",
														  "agent","symbol");
	m_ontology->new_sort("iba-prediction-confidence","any",2,"prediction","any",
														     "confidence","float");

	m_ontology->new_sort("request-casebase-size","any",0);

	m_ontology->new_sort("jbartering-cases","any",1,"cases","any");

	load(fp);
} /* AW_NOOSBrain::AW_NOOSBrain */ 


AW_NOOSBrain::~AW_NOOSBrain()
{
	m_casebase=0;
} /* AW_NOOSBrain::~AW_NOOSBrain */ 


bool AW_NOOSBrain::load(FILE *fp) 
{
	int nsources=0,i;
	char filename[256];
	FILE *noos_fp;

	if (!AW_TracerBrain::load(fp)) return false;

	if (1!=fscanf(fp,"%i",&nsources)) return false;

	for(i=0;i<nsources;i++) {
		if (1!=fscanf(fp,"%s",filename)) return false;

		noos_fp=fopen(filename,"rb");
		if (noos_fp==0) return false;
		if (!m_memory->ImportNOOS(noos_fp,m_ontology)) return false;
		fclose(noos_fp);
	} /* for */ 

	if (1!=fscanf(fp,"%i",&nsources)) return false;

	for(i=0;i<nsources;i++) {
		if (1!=fscanf(fp,"%s",filename)) return false;

		noos_fp=fopen(filename,"rb");
		if (noos_fp==0) return false;
		if (!m_casebase->ImportNOOS(noos_fp,m_ontology)) return false;
		fclose(noos_fp);
	} /* for */ 

	return true;
} /* AW_NOOSBrain::load */ 


bool AW_NOOSBrain::save(FILE *fp) 
{
	if (!AW_TracerBrain::save(fp)) return false;

	fprintf(fp,"0\n");
	fprintf(fp,"0\n");

	return true;
} /* AW_NOOSBrain::save */ 


bool AW_NOOSBrain::is_a(char *c)
{
	if (strcmp(c,"AW_NOOSBrain")==0) return true;

	return AW_TracerBrain::is_a(c);
} /* AW_NOOSBrain::is_a */ 


bool AW_NOOSBrain::is_a(Symbol *c)
{
	if (c->cmp("AW_NOOSBrain")) return true;

	return AW_TracerBrain::is_a(c);
} /* AW_NOOSBrain::is_a */ 


